import 'package:scoped_model/scoped_model.dart';
import '../Models/banner.dart';
import 'dart:async';

import 'dart:convert';
import '../Common/api.dart';
import 'package:http/http.dart' as http;

class ScopedBanner extends Model {
  final List<HomeBanner> _banners = [];

  List<HomeBanner> get banners {
    return _banners;
  }

  bool homeLoadingStatus = false;

  Future<void> getHomeData() async {
    homeLoadingStatus = true;
    notifyListeners();
    final http.Response response = await http.get(API.server + API.homePage);
    try {
      Map<String, dynamic> result = json.decode(response.body);
      result['bannerVOs'].forEach((element) {
        final uri = element['path']
            .toString(); // element['path'].toString().replaceFirst(new RegExp(r'\['), '%5b').replaceFirst(new RegExp(r'\]'), '%5d')
        final encode = Uri.encodeFull(uri); // '.0001'
        final HomeBanner model = HomeBanner(
            name: element['name'],
            path: '$encode',
            redirectUrl: element['redirectUrl']);
        _banners.add(model);
      });
      print(_banners);
      homeLoadingStatus = false;
      notifyListeners();
    } catch (error) {
      homeLoadingStatus = false;
      notifyListeners();
    }
  }
}
